home *** CD-ROM | disk | FTP | other *** search
- //==============================================================================
- // KEYQUENCER GLUE SAMPLE - VERSION 1.2.2 - DECEMBER 1995
- // By Alessandro Levi Montalcini <alm@torino.alpcom.it>
- // ©1994-96 Binary Software, Inc. <binarysoft@eworld.com>
- // This text looks best in monaco 9 font, 4 spaces per tab, no wrapping
- //==============================================================================
-
- #include "Extension.h"
- #include "KQGestalt.h"
- #include "KQPPCGlue.h"
-
- //==============================================================================
-
- short BuildMacrosMenu (MenuHandle mhandle);
- void ExecuteMacroFromMenu (short menu, short item);
- void DoMenu (long selection);
-
- //==============================================================================
-
- short gRunning = true;
-
- //==============================================================================
- // Build a menu containing a list of all the installed KeyQuencer macros:
-
- short BuildMacrosMenu(MenuHandle mhandle)
- {
- GluePtr glue; // points to the KeyQuencer glue record containing callback routine pointers
- short *sorted; // an array of shorts to be used as indexes when getting the macro names
- short count; // number of KeyQuencer macros that are currently installed
- short index; // index into the sorted indexes array
- short macroIndex; // index passed to GetMacroInfo
- short keyCode; // receives the key code of the macro's keystroke in the word's high byte
- short modifiers; // receives the modifiers of the macro's keystroke
- Str31 name; // receives the name of the macro
-
- count = 0;
-
- // get a pointer to the KeyQuencer glue (NIL if KQ is not installed):
- glue = GetKeyQuencerGlue();
-
- // glue version 5 is required to get the macro names:
- if(glue!=0L && glue->glueRecVers >= 5)
- {
- // get the number of installed macros:
- count = CallKQCountMacrosProc(glue->CountMacros);
-
- // build a sorted index of all the installed macros:
- sorted = (short*)CallKQBuildSortedIndexProc(glue->BuildSortedIndex);
- if(sorted)
- {
- // add each macro name to the menu:
- for(index=0; index<count; ++index)
- {
- // get the right index from our sorted index:
- macroIndex = sorted[index];
-
- // get the macro's name:
- CallKQGetMacroInfoProc(glue->GetMacroInfo, macroIndex, name, &keyCode, &modifiers);
-
- // add the name to the menu if it's not empty:
- // (use AppendMenu/SetItem to ignore metacharacters)
- if(name[0]>0)
- {
- AppendMenu(mhandle, "\p ");
- SetMenuItemText(mhandle, CountMItems(mhandle), name);
- }
- }
-
- // dispose the sorted index, which was created as a Ptr in the sys heap:
- DisposePtr((Ptr)sorted);
- }
- }
- return count;
- }
-
- //==============================================================================
- // Execute a macro whose name is stored as a menu item title:
-
- void ExecuteMacroFromMenu(short menu, short item)
- {
- MenuHandle mhandle; // menu handle of the macros menu
- Handle macro; // a handle containing the text of the macro, created in the sys heap
- GluePtr glue; // points to the KeyQuencer glue record containing callback routine pointers
- short macroErr; // nonzero if a syntax error was found in the macro or the queue was full
- Str255 name; // name of the macro, as stored in the menu item (a menu item could be longer than 31 chars)
-
- // get a pointer to the KeyQuencer glue (NIL if KQ is not installed):
- glue = GetKeyQuencerGlue();
-
- // glue version 5 is required to get the macro names:
- if(glue!=0L && glue->glueRecVers >= 5)
- {
- // get the menu handle:
- mhandle = GetMenuHandle(menu);
- if(mhandle)
- {
- // get the menu item:
- GetMenuItemText(mhandle, item, name);
-
- // get the KeyQuencer macro whose name matches the menu item:
- macro = CallKQGetNamedMacroProc(glue->GetNamedMacro, name);
- if(macro)
- {
- // execute the macro:
- macroErr = CallKQExecuteScriptProc(glue->ExecuteScript, macro);
-
- // dispose of the macro (it's a handle created in the sys heap):
- DisposeHandle(macro);
- }
- }
- }
- }
-
- //==============================================================================
- // This main routine implements a very basic event loop which only handles
- // menu selections from the mouse and the keyboard. It is way too stupid to
- // be commented...
-
- void main(void)
- {
- Handle mbar;
- MenuHandle mhandle;
- WindowRef window;
- long selection;
- short where;
- EventRecord event;
-
- MaxApplZone();
- MoreMasters();
- InitGraf(&qd.thePort);
- InitFonts();
- InitWindows();
- InitMenus();
- TEInit();
- InitCursor();
- InitDialogs(0L);
- FlushEvents(everyEvent, 0);
-
- mbar = GetNewMBar(128);
- if(mbar)
- {
- SetMenuBar(mbar);
- DrawMenuBar();
- mhandle = GetMenuHandle(130);
- (void)BuildMacrosMenu(mhandle);
-
- while(gRunning)
- {
- if(WaitNextEvent(everyEvent, &event, 30L, 0L))
- {
- switch(event.what)
- {
- case mouseDown:
- where = FindWindow(event.where, &window);
- switch(where)
- {
- case inMenuBar:
- selection = MenuSelect(event.where);
- if(selection) DoMenu(selection);
- HiliteMenu(0);
- break;
- }
- break;
-
- case keyDown:
- if(event.modifiers & cmdKey)
- {
- selection = MenuKey((unsigned char)event.message);
- if(selection) DoMenu(selection);
- HiliteMenu(0);
- }
- break;
- }
- }
- }
- }
- ExitToShell();
- }
-
- //==============================================================================
- // Menu selection switch:
-
- void DoMenu(long selection)
- {
- short menu, item;
-
- menu = (short)(selection>>16);
- item = (short)(selection&0xFFFF);
- switch(menu)
- {
- case 128:
- // apple menu not supported in this sample
- break;
- case 129:
- if(item==1) gRunning = false;
- break;
- case 130:
- ExecuteMacroFromMenu(menu, item);
- break;
- }
- }
-
- //==============================================================================
-